package com.amazon.service;

import com.amazon.bo.User;
import com.amazon.bo.enums.UserType;
import com.amazon.dao.UserDao;
import com.amazon.web.util.Send253SMS;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import com.amazon.bo.UserSession;
import javax.annotation.Resource;
import java.io.Console;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import com.amazon.dao.UserSessionDao;
/**
 * Created by anhui on 2018/1/1.
 */
@Service("userService")
public class UserService {

    @Resource
    private UserDao userDao;
    @Resource
    private SMSCodeService smsCodeService;
    @Resource
    private Send253SMS send253SMS;
    @Resource
    I18nService i18nService;
    @Resource
    UserSessionDao userSessionDao;

    public Map<String,Object> login(Map<String,Object> pms){
        User u= userDao.userLogin(pms.get("username").toString(),pms.get("pwd").toString());
        Map<String,Object> mp=new HashMap<>();
        if(u!=null){
            mp.put("username",u.getUsername());
            mp.put("realname",u.getRealName());
            mp.put("pwd",u.getPasswordHash());
        }
        return mp;
    }

    public User getUserByOpenId(String openId){

        User u  = userDao.getUserByOpenId(openId);
        if(u!=null){
            return u;
        }
        return null;
    }
    public void updateUser(User user) {
        userDao.update(user);
    }

    public User getUserByUsername(String username){

        User u  = userDao.getUserByUsername(username);
        if(u!=null){
            return u;
        }
        return null;
    }

    public UserSession createUserSession(long userId) {
        UserSession userSession = userSessionDao.getUserSessionByUserId(userId);
        if (userSession != null) {
            userSession.setToken(UUID.randomUUID().toString());
            userSessionDao.update(userSession);
        } else {
            userSession = new UserSession();
            userSession.setUserId(userId);
            userSession.setToken(UUID.randomUUID().toString());
            userSessionDao.add(userSession);
        }
        return userSession;
    }


    @Scheduled(cron = "0 45 23 ? * *")
    public Object test2(){

        return null;
    }

    public User getUserByToken(String token) {
        UserSession userSession = userSessionDao.getUserSessionByToken(token);
        if (userSession == null) {
            return null;
        }
        return userDao.getById(userSession.getUserId());
    }

    public List<User> getUserList(final UserType userType, final int page, final int pageSize){
        return userDao.getUserList(userType,page,pageSize);
    }

    public List<User> getShopUserList(final UserType userType,String conn, final int page, final int pageSize){
        return userDao.getShopUserList(userType,conn,page,pageSize);
    }

    public boolean saveUser(User u){
        return userDao.saveUser(u);
    }
}